<?php

/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/3/6
 * Time: 下午2:54
 */
namespace App\WsController;

use App\Model\User\UserModel;
use App\Msg\MsgCode;
use App\Msg\MsgInfo;
use App\Server\Task\UserOutlineTask;
use App\Utility\BaseException;
use App\Utility\Common;
use EasySwoole\Core\Http\Message\Status;
use EasySwoole\Core\Socket\AbstractInterface\WebSocketController;
use EasySwoole\Core\Swoole\Task\TaskManager;

class WsAbstract extends WebSocketController {
	public $token = "";
	public $userinfo = null;
	function actionNotFound(?string $actionName) {
		$this->writeJson ( Status::CODE_NOT_FOUND, null, MsgInfo::ACTION_NOT_FOUND );
	}
	function onRequest(?string $actionName): bool {
		$content = $this->request ()->getArg ( 'content' );
		if (isset ( $content ['Token'] ) && ! empty ( $content ['Token'] )) {
			$this->token = $content ['Token'];
			
			$userInfo = (new UserModel ())->getUserByToken ( $this->token );
			$this->userinfo = $userInfo;
			if ($userInfo->getUid ()) {
				return true;
			} else {
				$this->writeJson ( MsgCode::USER_NOT_LOGIN, null, MsgInfo::User ( MsgCode::USER_NOT_LOGIN ) );
				return false;
			}
		} else {
			$this->writeJson ( MsgCode::TOKEN_IS_NULL, null, MsgInfo::User ( MsgCode::TOKEN_IS_NULL ) );
			return false;
		}
	}
	static public function onOpen(\swoole_server $server, $fd, $reactorId) {
		(new Common ())->E ( "reactorId = {$reactorId} ,fd = {$fd}------------>执行on open" . PHP_EOL );
	}
	static public function onClose(\swoole_server $server, $fd, $reactorId) {
		$token = (new UserModel ())->getTokenByFd ( $fd );
		if ($token) {
			$userInfo = (new UserModel ())->getUserByToken ( $token );
			if ($userInfo->getUid ()) {
				$flag1 = (new UserModel ())->delUserFromCache ( $token );
				if ($flag1) {
					(new Common ())->E ( "清除" . $userInfo->getPhone () . " token =>userinfo success" );
				} else {
					(new Common ())->E ( "清除" . $userInfo->getPhone () . " token =>userinfo error" );
				}
				$flag2 = (new UserModel ())->delFdByUid ( $userInfo->getUid () );
				
				if ($flag2) {
					(new UserModel ())->onLineNumDown ();
					(new Common ())->E ( "清除" . $userInfo->getPhone () . " uid=>fd success" );
				} else {
					(new Common ())->E ( "清除" . $userInfo->getPhone () . " uid=>fd error" );
				}
				$flag3 = (new UserModel ())->delTokenByFd ( $fd );
				if ($flag3) {
					(new Common ())->E ( "清除" . $userInfo->getPhone () . " fd=>token success" );
				} else {
					(new Common ())->E ( "清除" . $userInfo->getPhone () . " fd=>token error" );
				}
				TaskManager::async ( (new UserOutlineTask ( [ 
						"userinfo" => $userInfo->toArray () 
				] )) );
			}
		}
		(new Common ())->E ( "reactorId = {$reactorId} ,fd = {$fd}------------>执行on cloce"  );
	}
	static public function onConnect(\swoole_server $server, $fd, $reactorId) {
		(new Common ())->E ( "reactorId = {$reactorId} ,fd = {$fd}------------>执行on connect" );
	}
	function writeJson($statusCode = 200, $result = null, $msg = null) {
		$data = Array (
				"code" => $statusCode,
				"result" => $result,
				"msg" => $msg 
		);
		$this->response ()->write ( json_encode ( $data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ) );
	}
	protected function onException(\Throwable $throwable, $actionName): void {
		// 若重载实现了onException 方法，那么控制器内发生任何的异常，都会被该方法拦截，该方法决定了如何向客户端响应
		$this->writeJson ( $throwable->getCode (), [ 
				'info' => $actionName . "error" 
		], $throwable->getMessage () );
	}
	public function throwExp($code, $msg) {
		throw new BaseException ( [ 
				'msg' => $msg,
				'code' => $code 
		] );
	}
	public function checkViewForm(Bool $logicStatic, int $code, string $msg = "", $action = "User") {
		if (empty ( $msg )) {
			$msg = MsgInfo::$action ( $code );
		}
		if (! $logicStatic) {
			throw new BaseException ( [ 
					'msg' => $msg,
					'code' => $code 
			] );
		}
		return $this;
	}
	public function MessageJson($statusCode = 200, $result = null, $msg = null) {
		$data = Array (
				"code" => $statusCode,
				"result" => $result,
				"msg" => $msg 
		);
		return json_encode ( $data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
	}
}